home *** CD-ROM | disk | FTP | other *** search
- /*
- **
- ** ParseFullPathname
- **
- ** Takes a *complete* full pathname and turns it into a DirID and VRefNum pair.
- ** This does not handle partial pathnames because it assumes that the first element
- ** in the pathname is volume name.
- **
- ** to recurse or not to recurse, that is the question... (not)
- **
- ** This is an MPW Tool - build it accordingly.
- **
- ** The #if immediately preceding the #include's allows you to use pre-compiled headers, speeding
- ** the compile-debug-compile cycle significantly. after you compile this the first time, set the
- ** 1 to a 0 to use the dump file...
- **
- ** Neil Day
- ** MacDTS
- ** Apple Computer, Inc.
- **
- */
-
- #if 0
- #include <stdio.h> // Compiler Interfaces
- #include <files.h>
- #include <String.h>
- #include <Strings.h>
- #include <Memory.h>
- #include <Errors.h>
- #pragma dump "CDump"
- #else
- #pragma load "CDump"
- #endif
-
- typedef struct { // a handy info record
- OSErr error;
- FSSpec spec;
- } specRec;
-
- specRec *ParseFullPathname (char *pathname); // function prototypes
- char *firstElement (char *pn);
-
- main (argc,argv)
- int argc;
- char **argv;
- {
- specRec *here;
-
- Debugger ();
-
- if (argc != 2) {
- printf ("calling format: pfpn <pathname>\n");
- return;
- }
-
- here = ParseFullPathname (argv[1]);
- if (!here->error)
- printf ("Specified by the VRefNum %d and DirID %d\n", here->spec.vRefNum, here->spec.parID);
- else
- printf ("There was an error %d while getting pathname\n",here->error);
-
- DisposPtr ((Ptr) here);
- printf ("disposed of a specRec\n");
- Debugger ();
- }
-
- char *firstElement (char *pn)
- {
- int cChar = 0;
- char *substring;
-
- while (*(pn+cChar) != ':' && *(pn + cChar) != '\0') // find the first element
- ++cChar;
-
- if (*(pn + cChar)) {
- substring = NewPtrClear (++cChar+1); // allocate space result
- printf ("Allocated a string\n");
- BlockMove ((Ptr) pn, (Ptr) substring, cChar); // move the first element into result
- BlockMove ((Ptr) pn+cChar, (Ptr) pn, strlen(pn) - cChar+1); // delete the first element if there is one
- } else
- substring = NULL; // no result
-
- return substring;
- }
-
- specRec *ParseFullPathname (char *pathname)
- {
- char *current;
- specRec *rRec;
- OSErr err;
- HParmBlkPtr vInfo;
- CInfoPBPtr dInfo;
-
- rRec = (specRec *) NewPtrClear (sizeof (specRec));
- rRec->spec.parID = fsRtDirID; // start at the top of the volume
- printf ("Allocated a specRec\n");
- current = firstElement (pathname);
- if (!current) {
- rRec->error = fnfErr;
- return rRec;
- }
-
- vInfo = (HParmBlkPtr) NewPtrClear (sizeof (HParamBlockRec)); // resolve vRefNum
- printf ("Allocated a paramblock\n");
- vInfo->volumeParam.ioNamePtr = c2pstr (current);
- vInfo->volumeParam.ioVolIndex = -1;
- err = PBHGetVInfo (vInfo,false);
- rRec->spec.vRefNum = vInfo->volumeParam.ioVRefNum;
- DisposPtr ((Ptr) vInfo);
- printf ("Disposed of a paramblock\n");
- if (current){
- DisposPtr ((Ptr) current);
- printf ("Disposed of a string\n");
- }
- if (err) {
- rRec->error = err;
- return rRec;
- }
-
- dInfo = (CInfoPBPtr) NewPtrClear (sizeof (CInfoPBRec));
- printf ("Allocated a Cparamblock\n");
-
- while (current = firstElement(pathname)) { // Walk down the pathname
- *(current + (strlen(current)-1)) = '\0'; // get rid of the colon
- dInfo->dirInfo.ioFDirIndex = 0;
- dInfo->dirInfo.ioDrDirID = rRec->spec.parID;
- dInfo->dirInfo.ioVRefNum = rRec->spec.vRefNum;
- dInfo->dirInfo.ioNamePtr = c2pstr (current);
- err = PBGetCatInfo (dInfo,false);
- DisposPtr ((Ptr) current);
- printf ("Disposed of a string\n");
- if (err) {
- rRec->error = err;
- break;
- }
- rRec->spec.parID = dInfo->dirInfo.ioDrDirID;
- }
- DisposPtr ((Ptr) dInfo);
- printf ("Disposed of a Cparamblock\n");
- return rRec;
- }